<template>
	<view class="container">
		<view class="userBox">
			<image src="@/static/image/defaultAvatar.png" mode="aspectFill" class="avatar" v-if="!is_login"></image>
			<image :src="domain + userinfo.avatar" mode="aspectFill" class="avatar" @click="routerPage('/pages/user/setting')" v-else></image>
			<view class="msg">
				<view class="name" v-if="is_login">{{userinfo.nickname}}<text class="lev" v-if="userinfo.level > 0">{{userinfo.level}}</text></view>
				<view class="name" v-else><uni-tag text="登录" type="success" @click="routerLogin()"/></view>
				<view class="desc" v-if="is_login">手机号：{{userinfo.mobile}}</view>
			</view>
			<uni-icons type="gear" size="26" class="setting" color="#ffffff" @click="routerPage('/pages/user/setting', true)"></uni-icons>
		</view>
		<view class="listBox">
			<view class="item" @click="routerPage('/pages/user/upgrade', true)">
				<view class="tit"><uni-icons type="vip" size="24"></uni-icons>会员升级</view>
				<uni-icons type="right" size="20" color="#999999"></uni-icons>
			</view>
			<view class="item" @click="routerPage('/pages/user/authlogs', true)">
				<view class="tit"><uni-icons type="auth" size="24"></uni-icons>升级审核</view>
				<uni-icons type="right" size="20" color="#999999"></uni-icons>
			</view>
			<view class="item" @click="routerPage('/pages/user/setting', true)">
				<view class="tit"><uni-icons type="compose" size="24"></uni-icons>会员资料</view>
				<uni-icons type="right" size="20" color="#999999"></uni-icons>
			</view>
			<view class="item" @click="routerPage('/pages/user/resetpwd', true)">
				<view class="tit"><uni-icons type="locked" size="24"></uni-icons>修改密码</view>
				<uni-icons type="right" size="20" color="#999999"></uni-icons>
			</view>
			<view class="item" @click="routerPage('/pages/user/share', true)">
				<view class="tit"><uni-icons type="staff" size="24"></uni-icons>分享中心</view>
				<uni-icons type="right" size="20" color="#999999"></uni-icons>
			</view>
			<view class="item" @click="routerPage('/pages/other/knowledge', true)">
				<view class="tit"><uni-icons type="color" size="24"></uni-icons>等级知识</view>
				<uni-icons type="right" size="20" color="#999999"></uni-icons>
			</view>
			<view class="item" @click="routerPage('/pages/other/goodsroom', true)" v-if="show_goods">
				<view class="tit"><uni-icons type="shop" size="24"></uni-icons>商品库</view>
				<uni-icons type="right" size="20" color="#999999"></uni-icons>
			</view>
			<view class="item" @click="routerPage('/pages/other/help', false)">
				<view class="tit"><uni-icons type="help" size="24"></uni-icons>帮助中心</view>
				<uni-icons type="right" size="20" color="#999999"></uni-icons>
			</view>
		</view>
		<button class="primary" @tap="logout()" v-if="is_login">退出登录</button>
	</view>
</template>

<script>
	import common from '@/static/js/common.js';
	export default {
		data() {
			return {
				domain: getApp().globalData.DOMAIN,
				userinfo: {},
				is_login: false,
				show_goods: false
			}
		},
		onShow() {
			if (uni.getStorageSync('token') && uni.getStorageSync('token') != '') {
				this.is_login = true
				this.getUserMsgInfo()
			}
		},
		methods: {
			getUserMsgInfo() {
				let that = this;
				common.api({
					method: 'GET',
					url: '/mobile/user/index',
					success:(res) => {
						if (res.code != 0) {
							uni.showToast({ title: res.msg, icon: 'none' });
							return false;
						}
						that.userinfo = res.data
						that.show_goods = (res.data.level>=res.data.highest_level)
						uni.setStorageSync('user_id', res.data.id)
					}
				});
			},
			routerPage(url, isLogin) {
				if (isLogin && !this.is_login) {
					uni.showModal({
						title: '温馨提示',
						content: '请先登录',
						confirmText: '去登录',
						success: function (res) {
							if (res.confirm) {
								uni.navigateTo({url: '/pages/login/index'})
							}
						}
					});
				} else {
					uni.navigateTo({url:url})
				}
			},
			routerLogin() {
				uni.reLaunch({url:'/pages/login/index'})
			},
			logout() {
				let that = this;
				common.api({
					method: 'POST',
					url: '/mobile/user/logout',
					success:(res) => {
						if (res.code != 0) {
							uni.showToast({ title: res.msg, icon: 'none' });
							return false;
						}
						uni.reLaunch({url:'/pages/login/index'})
					}
				});
			}
		}
	}
</script>

<style scoped lang="less">
	.userBox {
		height: 260rpx;
		background: url(@/static/image/bg-1.jpg) no-repeat center;
		background-size: 100% 100%;
		padding: 0 3vw;
		display: flex;
		align-items: center;
		position: relative;
		.avatar {
			width: 160rpx;
			height: 160rpx;
			border-radius: 50%;
			margin-right: 30rpx;
		}
		.setting {
			position: absolute;
			right: 30rpx;
			top: 16rpx;
		}
		.msg {
			color: #fff;
			.name {
				font-size: 35rpx;
				font-weight: bolder;
				display: flex;
				align-items: center;
				margin-bottom: 10rpx;
				.lev {
					margin-left: 30rpx;
					width: 36rpx;
					height: 54rpx;
					font-weight: normal;
					font-size: 26rpx;
					line-height: 50rpx;
					padding: 10rpx 14rpx 0 40rpx;
					text-align: center;
					background: url(@/static/image/level-bg.png) no-repeat center;
					background-size: 100% 100%;
				}
			}
		}
	}
	.listBox {
		background-color: #fff;
		margin-top: 30rpx;
		.item {
			height: 88rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 3vw;
			&+.item {
				border-top: 2rpx solid #eee;
			}
			.tit {
				display: flex;
				align-items: center;
				.uni-icons {
					margin-right: 10rpx;
				}
			}
			.arrow {
				height: 30rpx;
			}
		}
	}
	.primary {
		width: 88vw;
		margin: 40rpx auto;
	}
</style>
